// pages/discover/discover.js 
import API from '../../utils/api.js'
const App = getApp();
Page({
  data: {
    icon_like: '../../img/dis2.png',
    icon_unlike: '../../img/commit.png',
    count: 0, //点赞数量
    like: false, //是否已点赞
    clickNav: 1,
    animation: 1,
    count: 0,
    faxiantop: [],
    tuiloading: 1,
    page: 1,
    getfaxians: [],
    getfaxianRight: [],
    getfaxianLeft: [],
    scroll_height: 0,
    getQuanzi: [],
    b: -1,
    navList: ['圈子', '推荐', '附近', '最热'],
    num: 0,
    left: '8%',
    prompt: '上拉加载更多',
    isUpPull: false,
    scrollTop: 0,
    isDisabled: -1,
    attentiontext: "+关注",
    index: 1,
    videoimage: "block" //默认显示封面
  },
  //点击播放按钮，封面图片隐藏,播放视频
  bindplay: function(e) {
    this.setData({
        tab_image: "none"
      }),
      this.videoCtx.play()
  },
  onReady() {
    this.videoCtx = wx.createVideoContext('myVideo')
  },
  //进入大圈
  bigcircle() {
    wx.navigateTo({
      url: '/pages/Greatcircle/Greatcircle',
    })
  },
  //点赞
  onLike(e) {
    console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id
    // wx.vibrateShort() //手机振动API
    // this.animation = wx.createAnimation({
    //   duration: 300, // 动画持续时间，单位 ms
    //   timingFunction: 'linear', // 动画的效果
    //   delay: 10, // 动画延迟时间，单位 ms
    //   transformOrigin: '50% 50%' // 动画的中心点
    // })
    let like = this.properties.like
    let count = this.properties.count
    count = like ? count - 1 : count + 1
    // if (like) {
    //   setTimeout(function () {

    //     this.animation.scale(1.5).step();
    //     this.animation.scale(1.0).step();
    //     this.setData({
    //       animation: this.animation.export()
    //     });

    //   }.bind(this), 50);
    // }
    this.setData({
      count,
      like: !like
    })
    var data = {
      circleid: id,
      type: 1
    }
    API.cicrlComment(data).then(res => {
      if (res.ret == 0) {
        wx.showToast({
          title: '点赞成功',
        })
      }
    })
  },
  // 展开全部
  showTitle(e) {
    console.log(e.currentTarget.dataset.index)
    var index = e.currentTarget.dataset.index
    var type = e.currentTarget.dataset.type
    if (type == 0) {
      this.setData({
        b: index
      })
    } else {
      this.setData({
        b: -1
      })
    }

  },
  //导航栏到顶部固定
  onPageScroll: function(e) { //监听页面滚动
    this.setData({
      scrollTop: e.scrollTop
    })
  },
  // 进入圈子详情
  circle: function() {
    wx.navigateTo({
      url: '/pages/circle/circle',
    })
  },
  /**
   * 点击图片放大
   */
  enlarge: function(event) {
    var src = event.currentTarget.dataset.src; //获取data-src
    var imgList = event.currentTarget.dataset.list; //获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表　　　　
    })
  },
  /** 
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    that.setData({
      scroll_height: windowHeight * 750 / windowWidth - 788 - 30
    })
    // that.getcancerquanzi();
    that.getfaxiantop();
    this.getQuanzi()

  },
  // 去查看地图
  toMap(e) {
    console.log(e)
    var dataset = e.currentTarget.dataset
    var lat = Number(dataset.lat)
    var lng = Number(dataset.lng)
    var address = dataset.address
    console.log(lat)
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      scale: 13,
      name: address
    })
  },
  //关注人
  attention: function(res) {
    // wx.vibrateShort({
    //   success: function() {
    //     console.log("点击关注震动");
    //   }
    // });
    var index = res.currentTarget.dataset.index
    console.log(index);
    var id = res.currentTarget.dataset.id
    this.setData({
      isDisabled: index
    })
    var data = {
      cancerid: id,
      status: 1
    }
    API.cancerSome(data).then(res => {
      if (res.ret == 0) {
        this.setData({
          attentiontext: "已关注",

        })
        wx.showLoading({
          title: '关注成功',

        })
        setTimeout(function() {
          wx.hideLoading()
        }, 2000)
      }
    })
  },
  //点击我的关注
  interest: function() {
    this.setData({
      index: 1,
      getQuanzi: [],
      page: 1,
      prompt: '上拉加载更多',
    })
    this.getcancerquanzi();
  },
  //我的关注
  getcancerquanzi() {
    var that = this;
    // this.setData({
    //   getQuanzi: []
    // })
    var data = {
      limit: 30,
      page: this.data.page
    }
    API.getcancerquanzi(data).then(res => {
      setTimeout(function() {
        that.setData({
          animation: 2
        })
      }, 2000)
      console.log(res);
      if (res.data.length > 0) {
        this.setData({
          getQuanzi: this.data.getQuanzi.concat(res.data),
          tuiloading: 2,
        })
      } else {
        this.setData({
          prompt: '主人我是有底线的哟~'
        })
      }
    })
  },
  //最新
  newest() {
    this.setData({
      index: 2,
      getQuanzi: [],
      page: 1,
      prompt: '上拉加载更多'
    })
    this.getQuanzi();
  },
  //最新
  newest() {
    this.setData({
      index: 2,
      getQuanzi: [],
      page: 1,
      prompt: '上拉加载更多'
    })
    this.getQuanzi();
  },
  //头部图片
  getfaxiantop() {
    let that = this;
    wx.request({
      url: 'https://www.szhongtu.net/scxmcm/public/index.php/circletop',
      data: '',
      header: {
        'content-type': 'application/json'
      },
      method: 'GET',
      dataType: 'json',
      responseType: 'text',
      success: function(res) {
        console.log(res);
        console.log(123)
        console.log(res.data.data.data[0][0].images);
        that.setData({
          faxiantop: res.data.data.data,
          count: res.data.data.count
        })
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    // API.faxiantop().then(res => {
    //   console.log(res);
    //   console.log(res.data.data[0][0].circlename);
    //   if (res.ret == 0) {
    //     this.setData({
    //       faxiantop: res.data.data,
    //       count: res.data.count
    //     })

    //   }
    // })
  },
  getQuanzi() {
    //获取圈子
    var data = {
      limit: 10,
      page: this.data.page
    }
    API.getQuanzi(data).then(res => {
      console.log(res)
      console.log(res.data);
      if (res.ret == 0) {
        if (res.data.length > 0) {
          this.setData({
            getQuanzi: this.data.getQuanzi.concat(res.data),
            tuiloading: 2,
          })
        } else {
          this.setData({
            prompt: '主人我是有底线的哟~'
          })
        }
      }
    })
  },
  navTab(e) {
    var index = e.currentTarget.dataset.index
    console.log(index)
    var left = '8%'
    this.setData({
      getfaxianLeft: [],
      getfaxianRight: [],
      getQuanzi: [],
      page: 1,
      isUpPull: false
    })
    if (index == 0) {
      this.getQuanzi()
      left = '8%'
    } else if (index == 1) {
      this.getRecommendList(index)
      this.setData({
        prompt: '上拉加载更多'
      })
      left = '33%'
    } else if (index == 2) {
      this.getRecommendList(index)
      left = '58%'
    } else {
      this.getRecommendList(index)
      left = '83%'
      this.setData({
        prompt: '上拉加载更多'
      })
    }
    this.setData({
      num: index,
      left: left,
      tuiloading: 1
    })
  },
  //发帖子
  send() {
    wx.navigateTo({
      url: '/pages/sendOut/sendOut'
    })
  },
  //圈子详情
  detail: function(e) {
    var id = e.currentTarget.dataset.id;
    var data = {
      circleid: id
    }
    API.getdetail(data).then(res => {
      if (res.ret == 0) {
        wx.navigateTo({
          url: '/pages/particulars/particulars?id=' + id,
        })
        wx.setStorageSync('detail_name', res.data.username)
        wx.setStorageSync('detail_headimg', res.data.headimg)
      }
    })
  },
  getRecommendList(type) {
    var faxiandata = {
      type: type,
      page: this.data.page,
      limit: 10,
      latitude: wx.getStorageSync('lat'),
      longitude: wx.getStorageSync('lon')
    }
    API.getfaxian(faxiandata).then(res => {
      console.log(res.data.length);
      var result = res.data
      var getfaxianLeft = []
      var getfaxianRight = []
      var getfaxian = []
      if (res.ret == 0) {
        this.setData({
          tuiloading: 2,
          getfaxians: this.data.getfaxians.concat(res.data)
        })
        if (result.length > 0) {
          for (var k in result) {
            if (k % 2) {
              // 奇数
              getfaxianLeft.push(result[k])
            } else {
              // 偶数
              getfaxianRight.push(result[k])
            }
          }
          this.setData({
            getfaxianRight: this.data.getfaxianRight.concat(getfaxianRight),
            getfaxianLeft: this.data.getfaxianLeft.concat(getfaxianLeft)
          })
        } else {
          if (this.data.isUpPull) {
            this.setData({
              prompt: '主人我是有底线的哟~'
            })
          } else {
            this.setData({
              prompt: '暂无数据'
            })
          }

        }
      }
    })
  },

  onShow: function() {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      isUpPull: true
    })
    if (this.data.num !== 0) {
      this.getRecommendList(this.data.num);
    } else {
      this.getQuanzi();
    }

  },
  onReachBottom: function() {
    if (this.data.index == 1) {
      console.log("我的关注页面上拉")
      var page = ++this.data.page
      this.setData({
        page: page
      })
      this.getcancerquanzi();
    }
    if (this.data.index == 2) {
      console.log("最新页面上拉")
      var page = ++this.data.page
      this.setData({
        page: page
      })
      this.getQuanzi();
    }
    if (this.data.num !== 2 && this.data.num !== 0) {
      var page = ++this.data.page
      console.log(page)
      if (this.data.num != 0 || this.data.num != 2) {
        this.setData({
          page: page,
          isUpPull: true
        })
        this.getRecommendList(this.data.num)
      }
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(e) {
    console.log(e.target.dataset)
    var target = e.target.dataset
    console.log('分享')


        return {

          title: target.content,

          path: '/pages/particulars/particulars?id=' + target.id,

          imageUrl: target.imgs[0],

          success: function(res) {

            console.log("转发成功2222", res);



          },

          fail: (res) => {

            console.log("转发失败", res);

          }

        }


  }
})